'死亡组件，负责死亡动画的播放'

extends Node2D

@export var health_component: Node
@export var sprite: Sprite2D

func _ready() -> void:
	$GPUParticles2D.texture = sprite.texture
	health_component.died.connect(on_died)
	pass


func on_died() -> void:
	if self.owner == null || not self.owner is Node2D:
		self.queue_free.call_deferred()
		return
	var spanwn_position = self.owner.global_position
	var entities = get_tree().get_first_node_in_group("entities_layer")
	self.get_parent().remove_child(self)
	entities.add_child(self)
	self.global_position = spanwn_position
	$RanAudComponent.play_random()
	$AnimationPlayer.play("default")
	pass
